<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>低位龙头股分析系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <style>
        .header {
            background-color: #343a40;
            color: white;
            padding: 2rem 0;
            margin-bottom: 2rem;
        }

        .card {
            margin-bottom: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .card-header {
            background-color: #f8f9fa;
            font-weight: bold;
        }

        .result-container {
            display: none;
            margin-top: 2rem;
        }

        .chart-container {
            margin-top: 1.5rem;
            text-align: center;
        }

        .chart-container img {
            max-width: 100%;
            height: auto;
            margin-bottom: 1rem;
        }

        .loading {
            text-align: center;
            display: none;
            margin: 1rem 0;
        }
    </style>
</head>

<body>
    <div class="header">
        <div class="container">
            <h1>低位龙头股分析系统</h1>
            <p>识别和预测具有潜力的低位龙头股</p>
        </div>
    </div>

    <div class="container">
        <div class="row mb-4">
            <div class="col-12">
                <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
                    <div class="container-fluid">
                        <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                            data-bs-target="#navbarNav">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        <div class="collapse navbar-collapse" id="navbarNav">
                            <ul class="navbar-nav">
                                <li class="nav-item">
                                    <a class="nav-link active" href="/">首页</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/results">历史结果</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/backtesting">回测系统</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/news">市场新闻</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </nav>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">低位龙头股分析</div>
                    <div class="card-body">
                        <form id="analyzeForm">
                            <div class="mb-3">
                                <label for="price_ratio" class="form-label">价格比例（当前价格/历史高点）</label>
                                <input type="number" step="0.01" class="form-control" id="price_ratio"
                                    name="price_ratio" value="0.6">
                                <div class="form-text">建议值: 0.6 (不超过历史高点的60%)</div>
                            </div>
                            <div class="mb-3">
                                <label for="min_rise" class="form-label">最小历史涨幅（%）</label>
                                <input type="number" class="form-control" id="min_rise" name="min_rise" value="100">
                                <div class="form-text">建议值: 100 (历史上至少有100%的涨幅)</div>
                            </div>
                            <div class="mb-3">
                                <label for="max_pe" class="form-label">最大当前PE</label>
                                <input type="number" class="form-control" id="max_pe" name="max_pe" value="50">
                                <div class="form-text">建议值: 50 (当前PE不超过50)</div>
                            </div>
                            <div class="mb-3">
                                <label for="min_growth" class="form-label">最小收入增长率（%）</label>
                                <input type="number" class="form-control" id="min_growth" name="min_growth" value="10">
                                <div class="form-text">建议值: 10 (收入增长至少10%)</div>
                            </div>
                            <div class="mb-3">
                                <label for="min_rank" class="form-label">最小行业排名</label>
                                <input type="number" class="form-control" id="min_rank" name="min_rank" value="5">
                                <div class="form-text">建议值: 5 (行业排名前5)</div>
                            </div>
                            <div class="mb-3">
                                <label for="min_liquidity" class="form-label">最小日均成交额</label>
                                <input type="number" class="form-control" id="min_liquidity" name="min_liquidity"
                                    value="5000000">
                                <div class="form-text">建议值: 5000000 (日均成交额至少500万)</div>
                            </div>
                            <div class="mb-3">
                                <label for="max_downtrend" class="form-label">最大下跌趋势天数</label>
                                <input type="number" class="form-control" id="max_downtrend" name="max_downtrend"
                                    value="120">
                                <div class="form-text">建议值: 120 (下跌趋势不超过120天)</div>
                            </div>
                            <div class="mb-3">
                                <label for="top_n" class="form-label">返回结果数量</label>
                                <input type="number" class="form-control" id="top_n" name="top_n" value="10">
                            </div>
                            <button type="submit" class="btn btn-primary">开始分析</button>
                        </form>
                        <div class="loading" id="analyzeLoading">
                            <div class="spinner-border text-primary" role="status">
                                <span class="visually-hidden">Loading...</span>
                            </div>
                            <p>正在分析，请稍候...</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">预测下一批低位龙头股</div>
                    <div class="card-body">
                        <form id="predictForm">
                            <div class="mb-3">
                                <label for="count" class="form-label">预测数量</label>
                                <input type="number" class="form-control" id="count" name="count" value="5">
                            </div>
                            <button type="submit" class="btn btn-success">开始预测</button>
                        </form>
                        <div class="loading" id="predictLoading">
                            <div class="spinner-border text-success" role="status">
                                <span class="visually-hidden">Loading...</span>
                            </div>
                            <p>正在预测，请稍候...</p>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header">查看历史结果</div>
                    <div class="card-body">
                        <a href="/results" class="btn btn-info">查看历史分析结果</a>
                    </div>
                </div>
            </div>
        </div>

        <div class="result-container" id="analyzeResult">
            <h3>分析结果</h3>
            <div id="analyzeMessage" class="alert alert-success"></div>
            <div class="chart-container">
                <h4>低位龙头股评分排行</h4>
                <img id="scoresChart" src="" alt="评分排行">
            </div>
            <div class="chart-container">
                <h4>多维度评分对比</h4>
                <img id="radarChart" src="" alt="雷达图">
            </div>
            <div class="mt-3">
                <button class="btn btn-primary" onclick="window.location.reload()">返回</button>
            </div>
        </div>

        <div class="result-container" id="predictResult">
            <h3>预测结果</h3>
            <div id="predictMessage" class="alert alert-success"></div>
            <div class="chart-container">
                <h4>预测的下一批低位龙头股</h4>
                <img id="predictionChart" src="" alt="预测结果">
            </div>
            <div class="chart-container">
                <h4>预测低位龙头股行业分布</h4>
                <img id="industryChart" src="" alt="行业分布">
            </div>
            <div class="mt-3">
                <button class="btn btn-primary" onclick="window.location.reload()">返回</button>
            </div>
        </div>

        <!-- 添加最新新闻部分 -->
        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">最新市场新闻</h5>
                        <a href="/news" class="btn btn-sm btn-outline-primary">查看更多</a>
                    </div>
                    <div class="card-body p-0">
                        {% if latest_news %}
                        <div class="list-group list-group-flush">
                            {% for news in latest_news %}
                            <div class="list-group-item">
                                <div class="d-flex w-100 justify-content-between">
                                    <h6 class="mb-1">
                                        <a href="{{ news.url }}" target="_blank">{{ news.title }}</a>
                                    </h6>
                                    <small class="text-muted">{{ news.publish_time }}</small>
                                </div>
                                <div class="d-flex justify-content-between">
                                    <small class="text-success">{{ news.source }}</small>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                        {% else %}
                        <div class="p-4 text-center">
                            <p>暂无新闻数据</p>
                            <a href="/news" class="btn btn-outline-primary">
                                <i class="bi bi-arrow-right"></i> 前往获取新闻
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function () {
            // 分析表单提交
            $('#analyzeForm').on('submit', function (e) {
                e.preventDefault();

                // 显示加载中
                $('#analyzeLoading').show();

                // 发送请求
                $.ajax({
                    url: '/analyze',
                    type: 'POST',
                    data: $(this).serialize(),
                    success: function (response) {
                        $('#analyzeLoading').hide();

                        if (response.status === 'success') {
                            $('#analyzeMessage').text(response.message);
                            $('#scoresChart').attr('src', response.scores_chart);
                            $('#radarChart').attr('src', response.radar_chart);
                            $('#analyzeResult').show();
                            $('#analyzeForm').closest('.card').hide();
                            $('#predictForm').closest('.card').hide();
                        } else {
                            alert('分析失败: ' + response.message);
                        }
                    },
                    error: function () {
                        $('#analyzeLoading').hide();
                        alert('服务器错误，请稍后再试');
                    }
                });
            });

            // 预测表单提交
            $('#predictForm').on('submit', function (e) {
                e.preventDefault();

                // 显示加载中
                $('#predictLoading').show();

                // 发送请求
                $.ajax({
                    url: '/predict',
                    type: 'POST',
                    data: $(this).serialize(),
                    success: function (response) {
                        $('#predictLoading').hide();

                        if (response.status === 'success') {
                            $('#predictMessage').text(response.message);
                            $('#predictionChart').attr('src', response.prediction_chart);
                            $('#industryChart').attr('src', response.industry_chart);
                            $('#predictResult').show();
                            $('#analyzeForm').closest('.card').hide();
                            $('#predictForm').closest('.card').hide();
                        } else {
                            alert('预测失败: ' + response.message);
                        }
                    },
                    error: function () {
                        $('#predictLoading').hide();
                        alert('服务器错误，请稍后再试');
                    }
                });
            });
        });
    </script>
</body>

</html>